package com.hyx.api.service.impl.pc_admin;

import java.util.List;
import java.util.Arrays;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.hyx.api.service.pc_admin.IFileUploadService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.hyx.api.mapper.FileUploadMapper;
import com.hyx.api.domain.FileUpload;
import com.hyx.api.domain.bo.FileUploadBo;
import com.hyx.api.domain.vo.FileUploadVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;

/**
 * 文件管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-27
 */
@RequiredArgsConstructor
@Service
public class FileUploadServiceImpl implements IFileUploadService
{
    private final FileUploadMapper fileUploadMapper;

    /**
     * 查询文件管理
     *
     * @param id 文件管理主键
     * @return 文件管理
     */
    @Override
    public FileUploadVo selectFileUploadById(Long id)
    {
        //TODO 表以外的字段，写入vo
        return fileUploadMapper.selectVoById(id);
    }

    /**
     * 查询文件管理列表
     *
     * @param bo 文件管理
     * @return 文件管理
     */
    @Override
    public List<FileUploadVo> selectFileUploadList(FileUploadBo bo)
    {
        LambdaQueryWrapper<FileUpload> lqw = Wrappers.lambdaQuery();
        lqw.like(ObjectUtils.isNotEmpty(bo.getName()), FileUpload::getName, bo.getName());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getStatus()), FileUpload::getStatus, bo.getStatus());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getType()), FileUpload::getType, bo.getType());
        lqw.orderByDesc(FileUpload::getType);
        lqw.orderByDesc(FileUpload::getUploadTime);
        return fileUploadMapper.selectVoList(lqw);
    }

    /**
     * 新增文件管理
     *
     * @param bo 文件管理
     * @return 结果
     */
    @Override
    public int insertFileUpload(FileUploadBo bo)
    {
        FileUpload fileUpload = tranEntityBeforeSave(bo);
        validEntityBeforeSave(fileUpload);
        return fileUploadMapper.insert(fileUpload);
    }

    /**
     * 修改文件管理
     *
     * @param bo 文件管理
     * @return 结果
     */
    @Override
    public int updateFileUpload(FileUploadBo bo)
    {
        FileUpload fileUpload = tranEntityBeforeSave(bo);
        return fileUploadMapper.updateById(fileUpload);
    }

    /**
     * 批量删除文件管理
     *
     * @param ids 需要删除的文件管理主键
     * @return 结果
     */
    @Override
    public int deleteFileUploadByIds(Long[] ids)
    {
        return fileUploadMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(FileUpload entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     *   保存前的数据转换
     */
    private FileUpload tranEntityBeforeSave(FileUploadBo bo) {
        //TODO 做一些数据转换
        FileUpload fileUpload = new FileUpload();
        BeanUtils.copyProperties(bo, fileUpload);
        return fileUpload;
    }
}
